package com.konne.nightmare.FastPublicOpinion.ui.information.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpActivity;
import com.konne.nightmare.FastPublicOpinion.bean.MembersBean;
import com.konne.nightmare.FastPublicOpinion.bean.StudentTeamBean;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.MainPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.MembersManagerPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IMainView;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IMembersManagerView;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.MemberManagerFunDialog;
import com.konne.nightmare.FastPublicOpinion.ui.information.adapter.MembersManagerMAdapter;
import com.konne.nightmare.FastPublicOpinion.ui.information.adapter.StudentTeamAdapter;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.widget.QJActionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Create by ws.
 * description: 成员管理.
 */
public class MembersManagerActivity extends BaseMvpActivity<IMembersManagerView, MembersManagerPresenter> implements IMembersManagerView, MembersManagerMAdapter.MembersManagerListener {
    @BindView(R.id.stv_tab1)
    SuperTextView stv_tab1;
    @BindView(R.id.stv_tab2)
    SuperTextView stv_tab2;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.fab_add_team)
    View fab_add_team;
    private List<MembersBean> members;
    private List<StudentTeamBean> teams;

    public static void start() {
        ActivityUtils.startActivity(MembersManagerActivity.class);
    }

    @Override
    protected MembersManagerPresenter createPresenter() {
        return new MembersManagerPresenter();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_members_manager;
    }

    @Override
    public void initView() {
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this, true);
        ((QJActionBar) findViewById(R.id.action_bar)).setTitle("成员管理");
        stv_tab1.setSelected(true);
        stv_tab1.setOnClickListener(tabClick);
        stv_tab2.setSelected(false);
        stv_tab2.setOnClickListener(tabClick);
        viewPager.setAdapter(new MMAdapter());
        viewPager.setOffscreenPageLimit(2);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    setTabSel(stv_tab1, true);
                    setTabSel(stv_tab2, false);
                } else {
                    setTabSel(stv_tab1, false);
                    setTabSel(stv_tab2, true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        fab_add_team.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MemberManagerFunDialog(MembersManagerActivity.this).setMemberManagerFunListener(new MemberManagerFunDialog.MemberManagerFunListener() {
                    @Override
                    public void fun(int type) {
                        CreateGroupActivity.start(type);
                    }
                }).show();
            }
        });
    }

    private View.OnClickListener tabClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view == stv_tab1) {
                if (stv_tab1.isSelected()) {
                    return;
                }
                setTabSel(stv_tab1, true);
                setTabSel(stv_tab2, false);
                viewPager.setCurrentItem(0);
            } else if (view == stv_tab2) {
                if (stv_tab2.isSelected()) {
                    return;
                }
                setTabSel(stv_tab1, false);
                setTabSel(stv_tab2, true);
                viewPager.setCurrentItem(1);
            }
        }
    };

    private void setTabSel(SuperTextView tab, boolean isSel) {
        tab.setSelected(isSel);
        tab.setShaderEnable(isSel);
        tab.setTextColor(isSel ? 0xffffffff : 0xff344854);
        if(tab==stv_tab2 && isSel){
            fab_add_team.setVisibility(View.VISIBLE);
        }else {
            fab_add_team.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.init();
    }

    @Override
    public void action(int action) {
        mPresenter.action(action);
    }

    @Override
    public void updataMembers(List<MembersBean> members,int teacherSize) {
        this.members = members;
        stv_tab1.setText(String.format("全部成员(%d)",teacherSize));

        RecyclerView recyclerView = (RecyclerView) ((MMAdapter) viewPager.getAdapter()).getPrimaryItem(0);
        if (recyclerView == null) {
            return;
        }
        MembersManagerMAdapter managerMAdapter = (MembersManagerMAdapter) recyclerView.getAdapter();
        if (managerMAdapter == null) {
            managerMAdapter = new MembersManagerMAdapter(members);
            managerMAdapter.setMembersManagerListener(this);
            recyclerView.setAdapter(managerMAdapter);
        } else {
            managerMAdapter.setNewData(members);
        }
    }

    @Override
    public void updataStudentTeams(List<StudentTeamBean> teams,int studentSize) {
        this.teams=teams;
        stv_tab2.setText(String.format("学生分组(%d)",studentSize));

        RecyclerView recyclerView = (RecyclerView) ((MMAdapter) viewPager.getAdapter()).getPrimaryItem(1);
        if (recyclerView == null) {
            return;
        }
        StudentTeamAdapter studentTeamAdapter = (StudentTeamAdapter) recyclerView.getAdapter();
        if (studentTeamAdapter == null) {
            studentTeamAdapter = new StudentTeamAdapter(teams);
            recyclerView.setAdapter(studentTeamAdapter);
        } else {
            studentTeamAdapter.setNewData(teams);
        }
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return dialog;
    }

    @Override
    public void cancelLoadDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.cancel();
        }
    }

    class MMAdapter extends PagerAdapter {
        private View[] views;

        public MMAdapter() {
            views = new View[getCount()];
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        public View getPrimaryItem(int position) {
            return views[position];
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            RecyclerView recyclerView = new RecyclerView(MembersManagerActivity.this);
            container.addView(recyclerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            recyclerView.setLayoutManager(new LinearLayoutManager(MembersManagerActivity.this));
            views[position] = recyclerView;
            return recyclerView;
        }
    }
}
